package dkbta;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

public class Config{
	public static final String FRAME = "\n325623tedtgag";
	public static final String FAILURE_PREFIX = "failure#";
	public static final String RESULTS_PREFIX = "results#";

	private static final String CONFIG_FILE_NAME = "./files/conf/Config.ini";

	/**
	 * The configuration options are stored in a HashMap and in Properties to avoid the
	 * unneeded synchronization
	 */
	private static final Map<String, String> _config = new HashMap<String, String>();

	static{
		try{
			// Loading the properties
			Properties prop = new Properties();
			prop.load(new FileInputStream(CONFIG_FILE_NAME));
			// Copying into the hash
			for (Entry<Object, Object> e : prop.entrySet()){
				_config.put((String)e.getKey(), (String)e.getValue());
			}
		}catch(Exception e){
			// No point in logging the exception because if this has happened
			// the logger hasn't been initialized yet
			System.err.println("Unable to load configuration from: " + CONFIG_FILE_NAME);
			e.printStackTrace();
		}
	}

	/**
	 * Returns the value of the configuration option as a boolean if one was set, the
	 * default value otherwise
	 * 
	 * @param optionName The name of the option to be looked up
	 * @param defaultValue The default value to be returned
	 * @return The value of the configuration option or the default value if no value was
	 *         set
	 */
	public static boolean getBoolean(String optionName, boolean defaultValue){
		String value = _config.get(optionName);
		return value == null ? defaultValue : Boolean.parseBoolean(value);
	}

	/**
	 * Returns the value of the configuration option as a string if one was set, the
	 * default value otherwise
	 * 
	 * @param optionName The name of the option to be looked up
	 * @param defaultValue The default value to be returned
	 * @return The value of the configuration option or the default value if no value was
	 *         set
	 */
	public static String getString(String optionName, String defaultValue){
		String value = _config.get(optionName);
		return value == null ? defaultValue : value;
	}
	
	public static int getInt(String optionName, int defaultValue){
		String value = _config.get(optionName);
		return value == null ? defaultValue : Integer.parseInt(value);
	}
	
}
